package org.lsda.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lsda.job.LsdaAction;
import org.lsda.sched.LsdaScheduler;
import org.lsda.util.Util;
import org.quartz.CronTrigger;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * Servlet implementation class ScheduleTime
 */
// @WebServlet(name = "ScheduleTime", urlPatterns = {"ScheduleTime"})
public class ListTrigger extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(ListTrigger.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ListTrigger() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String jobName = request.getParameter("JobName");
		String groupName = request.getParameter("JobGroup");
		log.info(jobName);
		log.info(groupName);

		List<? extends Trigger> triggers = null;
		JsonObject json = new JsonObject();
		try {
			triggers = LsdaScheduler.getTriggers(groupName, jobName);
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			json.addProperty("Result", "ERROR");
			json.addProperty("Message", e.getMessage());
			log.fatal(e.getCause());
			PrintWriter writer = response.getWriter();
//			System.out.println(json.toString());
			writer.write(json.toString());
			return;
		}
		int total = 0;
		// TODO Auto-generated method stub

		JsonArray records = new JsonArray();
		for (Trigger trigger : triggers) {
			if (trigger != null) {
				if (trigger instanceof SimpleTrigger) {
					SimpleTrigger st = (SimpleTrigger) trigger;
					JsonObject record = new JsonObject();
					record.addProperty("JobGroup", groupName);
					record.addProperty("JobName", jobName);
					record.addProperty("TriggerName", st.getKey().getName());
					record.addProperty("TriggerGroup", st.getKey().getGroup());
					record.addProperty("SchedType", 0);
					record.addProperty("Start",
							Util.dateString(st.getStartTime()));
					record.addProperty("End", Util.dateString(st.getEndTime()));
					record.addProperty("Repeat", st.getRepeatCount());
					record.addProperty("Interval", st.getRepeatInterval());
					record.addProperty("TriggerDesc", st.getDescription());
					records.add(record);
					total++;
				} else if (trigger instanceof CronTrigger) {
					CronTrigger ct = (CronTrigger) trigger;
					JsonObject record = new JsonObject();
					record.addProperty("JobGroup", groupName);
					record.addProperty("JobName", jobName);
					record.addProperty("TriggerName", ct.getKey().getName());
					record.addProperty("TriggerGroup", ct.getKey().getGroup());
					record.addProperty("SchedType", 1);
					record.addProperty("Start",
							Util.dateString(ct.getStartTime()));
					record.addProperty("End",
							Util.dateString(ct.getStartTime()));
					record.addProperty("Crontab", ct.getCronExpression());
					record.addProperty("TriggerDesc", ct.getDescription());
					records.add(record);
					total++;
				}
			}
		}
		json.addProperty("Result", "OK");
		json.add("Records", records);
		json.addProperty("TotalRecordCount", total);
		PrintWriter writer = response.getWriter();
//		System.out.println(json.toString());
		writer.write(json.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
